//
//  MemberDetailViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/7.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger,MemberDetailVCCellType) {
    MemberDetailVCCellType_Sex,//性别
    MemberDetailVCCellType_Mobile,//手机
    MemberDetailVCCellType_CoachList,//指导教练
    MemberDetailVCCellType_Gym,//所在门店
    MemberDetailVCCellType_JoinDate,//加入时间
};

#import "MemberDetailViewController.h"
#import "I_Member.h"
#import "CommonTableViewCell.h"

@interface MemberDetailViewController ()
@property (nonatomic, copy) NSString *memberId;//
@property (nonatomic, strong) M_MemberDetail *memberDetail;
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation MemberDetailViewController


#pragma mark Class Method
+ (instancetype)controllerWithMemberId:(NSString *)memberId
{
    MemberDetailViewController *detailVC=[[MemberDetailViewController alloc] init];
    detailVC.memberId = memberId;
    return detailVC;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员信息";
    [self configUI];
    [self requestMemberDetail];
    _dataSource = @[@[@(MemberDetailVCCellType_Sex),@(MemberDetailVCCellType_Mobile)],@[@(MemberDetailVCCellType_CoachList),@(MemberDetailVCCellType_Gym)],@[@(MemberDetailVCCellType_JoinDate)]];
}

#pragma mark UI
- (void)configUI
{
    self.tableView.insetTop = 10;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
}


- (void)refreshTableHeader
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 95)];
    headerView.backgroundColor = [UIColor whiteColor];
    UIImageView *avaterImageView = [headerView addVCenterImageViewKeepLeft:20 width:60 height:60 imageName:nil];
    [avaterImageView setAvaterImageWithUrl:_memberDetail.member.memberAvatarUrl];
    [headerView addVCenterLabelKeepRight:20 text:_memberDetail.member.memberName textColor:HEXColor(@"333333") font:RegularFont(14)];
    [headerView addBottomLineViewWithLeftSpace:0 rightSpace:0];
    self.tableView.tableHeaderView = headerView;
}

#pragma mark NET
- (void)requestMemberDetail
{
    [self.view showLoading];
    WeakSelf(self);
    [I_Member requestMemberDetailWithMemberId:_memberId success:^(M_MemberDetail *memberDetail) {
        [weakself.view hideStatusView];
        weakself.memberDetail = memberDetail;
        [weakself refreshTableHeader];
        [weakself.tableView reloadData];
    } failure:^(NSError *error) {
        [weakself.view showError:error refreshBlock:^{
            [weakself requestMemberDetail];
        }];
    }];
}


#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_memberDetail) {
        return _dataSource.count + 1;
    }
    return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < _dataSource.count) {
        NSArray *arr = _dataSource[section];
        return arr.count;
    } else {
        return _memberDetail.member.cardTimesConsumeMonth.count + 2;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.showStyle = CommonTableViewShowStyleRightText;
    if (indexPath.section < _dataSource.count) {
        MemberDetailVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
        
        switch (cellType) {
            case MemberDetailVCCellType_Sex:
            {
                cell.leftLabel.text = @"性别";
                cell.rightLabel.text = _memberDetail.member.memberSex == MemberSexType_Female ? @"女" : @"男";;
            }
                break;
            case MemberDetailVCCellType_Mobile:
            {
                cell.leftLabel.text = @"联系电话";
                cell.rightLabel.text =  _memberDetail.member.memberMobile ? : @"未设置";
            }
                break;
            case MemberDetailVCCellType_CoachList:
            {
                cell.leftLabel.text = @"指导教练";
                NSString *coachListStr = @"";
                for (M_Coach *coach in _memberDetail.coachList) {
                    if (coachListStr.length > 0) {
                        coachListStr = [coachListStr stringByAppendingString:@"、"];
                    }
                    coachListStr = [coachListStr stringByAppendingString:coach.coachName];
                }
                cell.rightLabel.text = [NSString stringWithFormat:@"%@",coachListStr.length > 0 ? coachListStr : @"无"];
            }
                break;
            case MemberDetailVCCellType_Gym:
            {
                cell.leftLabel.text = @"所在门店";
                cell.rightLabel.text = RI.currentGym.gymName;
            }
                break;
            case MemberDetailVCCellType_JoinDate:
            {
                cell.leftLabel.text = @"加入时间";
                cell.rightLabel.text = [Toolkit stringFromDate:[NSDate dateWithTimeIntervalSince1970:_memberDetail.member.memberCreatedate/1000] formatterString:@"yyyy-MM-dd HH:mm"];;
            }
                break;
                
            default:
                break;
        }
    } else {
        if (indexPath.row == 0) {
            cell.leftLabel.text = @"课时统计";
            cell.leftLabel.font = RegularFont(17);
            
            cell.rightLabel.text = [NSString stringWithFormat:@"总：%ld 已上：%ld 剩余：%ld",(long)_memberDetail.member.cardTimes,(long)_memberDetail.member.cardTimesConsume,((long)_memberDetail.member.cardTimes - (long)_memberDetail.member.cardTimesConsume)];
        } else if (indexPath.row == 1) {
            cell.leftLabel.text = @"月份";
            cell.rightLabel.text = @"上课数";
        } else {
            M_CardTimesConsumeMonth *month = _memberDetail.member.cardTimesConsumeMonth[indexPath.row - 2];
            cell.leftLabel.text = month.monthStr;
            cell.rightLabel.text = [NSString stringWithFormat:@"%d",month.cardTimesConsume];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

@end
